package com.globallogic.neighbouringnumbers;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Determines the first smallest distance between beside values.
 */
public class NeighbouringNumbers {

    public static void main(String[] args) {
         int count = getCount();
         System.out.println("----------");
         System.out.print("Min Distance Index: " + getMinDistanceIdx(getArray(count), count));

    }

    private static int getCount() {
        int count = -1;
        try {
            BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Input size of array: n -> ");
            String inputString = bReader.readLine();
            count = Integer.parseInt(inputString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    private static int[] getArray(int count) {
        int array[] = new int[count];
        try {
            int i;
            BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
            String inputString;
            for (i = 0; i < count; i++) {
                System.out.print("Input value of array[" + i + "] -> ");
                inputString = bReader.readLine();
                array[i] = Integer.parseInt(inputString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return array;
    }

    /**
     * @param array - array of values.
     * @param n - count of values in array
     * @return -
     */
    private static int getMinDistanceIdx(int[] array, int n) {
        int iMDistance = Math.abs(array[1] - array[0]);
        int MDIdx = 0;
        int i;
        for (i = 2; i < n; i++) {
            if (iMDistance > Math.abs(array[i] - array[i - 1])) {
                iMDistance = Math.abs(array[i] - array[i - 1]);
                MDIdx = i - 1;
            }
        }
        return MDIdx;
    }
}
  